package com.yj.lottery_system.controller;

import com.yj.lottery_system.common.errorcode.ControllerErrorCodeConstants;
import com.yj.lottery_system.common.exception.ControllerException;
import com.yj.lottery_system.common.pojo.CommonResult;
import com.yj.lottery_system.common.utils.JacksonUtil;
import com.yj.lottery_system.controller.param.CreatePrizeParam;
import com.yj.lottery_system.controller.param.PageParam;
import com.yj.lottery_system.controller.result.FindPrizeListResult;
import com.yj.lottery_system.controller.result.FindPrizeListResult;
import com.yj.lottery_system.service.IPictureService;
import com.yj.lottery_system.service.IPrizeService;
import com.yj.lottery_system.service.dto.PageListDTO;
import com.yj.lottery_system.service.dto.PrizeDTO;
import jakarta.annotation.Resource;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;

import org.slf4j.Logger;
import org.springframework.web.multipart.MultipartFile;

import java.util.stream.Collectors;

@RestController
public class PrizeController {
    private static final Logger log = LoggerFactory.getLogger(PrizeController.class);

    @Resource
    private IPictureService pictureService;
    @Resource
    private IPrizeService prizeService;
    @RequestMapping("/pic/upload")
    public String uploadPic(MultipartFile file){
        return pictureService.savePicture(file);
    }

    /**
     * 创建奖品
     * RequestPart 接收表单数据
     * @param param
     * @param file
     * @return
     */
    @RequestMapping("/prize/create")
    public CommonResult<Long> createPrize(@Validated @RequestPart("param") CreatePrizeParam param,
                                          @RequestPart("prizePic") MultipartFile file) {
        //日志打印
        log.info("createPrize CreatePrizeParam: {}", JacksonUtil.writeValueAsString(param));
        //调用service服务
        Long id = prizeService.createPrize(param,file);
        return CommonResult.success(id);

    }
    @RequestMapping("/prize/find-list")
    public CommonResult<FindPrizeListResult> findPrizeList(PageParam param) {
        //日志打印
        log.info("findPrizeList PageParam: {}", JacksonUtil.writeValueAsString(param));
        //调用service服务
        PageListDTO<PrizeDTO> prizeList = prizeService.findPrizeList(param);
        return CommonResult.success(convertTOFindPrizeListResult(prizeList));
    }

    private FindPrizeListResult convertTOFindPrizeListResult(PageListDTO<PrizeDTO> prizeList) {
        if(null == prizeList) {
            throw new ControllerException(ControllerErrorCodeConstants.FIND_PRIZE_LIST_ERROR);
        }
        FindPrizeListResult findPrizeListResult = new FindPrizeListResult();
        findPrizeListResult.setTotal(prizeList.getTotal());
        findPrizeListResult.setRecords(
                prizeList.getRecords().stream()
                        .map(prizeDTO -> {
                            FindPrizeListResult.PrizeInfo prizeInfo = new FindPrizeListResult.PrizeInfo();
                            prizeInfo.setPrizeName(prizeDTO.getName());
                            prizeInfo.setDescription(prizeDTO.getDescription());
                            prizeInfo.setPrizeId(prizeDTO.getPrizeId());
                            prizeInfo.setImageUrl(prizeDTO.getImageUrl());
                            prizeInfo.setPrice(prizeDTO.getPrice());
                            return prizeInfo;
                        }).collect(Collectors.toList())
        );
        return findPrizeListResult;
    }
}
